unit ingresarPago;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ComCtrls;

type
  TfingresarPago = class(TForm)
    Panel1: TPanel;
    LabeledEdit1: TLabeledEdit;
    LabeledEdit2: TLabeledEdit;
    LabeledEdit3: TLabeledEdit;
    ComboBox1: TComboBox;
    Label1: TLabel;
    LabeledEdit4: TLabeledEdit;
    Panel2: TPanel;
    Button1: TButton;
    Button2: TButton;
    DateTimePicker1: TDateTimePicker;
    procedure Button1Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    procedure registrarPago;
    procedure limpiarFormulario;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fingresarPago: TfingresarPago;

implementation

uses datamodule, cuentaCorriente;
{$R *.dfm}

procedure TfingresarPago.Button1Click(Sender: TObject);
begin
  if ComboBox1.ItemIndex <> 0 then
  begin
    registrarPago;
  end
  else
  begin
    ShowMessage('Ingrese la forma de pago');
  end;
end;

procedure TfingresarPago.ComboBox1Change(Sender: TObject);
begin
  if ComboBox1.ItemIndex = 2 then
  begin
    LabeledEdit1.Enabled := true;
    LabeledEdit2.Enabled := true;
    LabeledEdit4.Enabled := true;
  end;
end;

procedure TfingresarPago.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  self.limpiarFormulario;
end;

procedure TfingresarPago.limpiarFormulario;
begin
  ComboBox1.ItemIndex := 0;
  LabeledEdit3.Text := '0';
  LabeledEdit2.Text := '';
  LabeledEdit1.Text := '';
  LabeledEdit4.Text := '';
  LabeledEdit1.Enabled := false;
  LabeledEdit2.Enabled := false;
  LabeledEdit4.Enabled := false;

end;

procedure TfingresarPago.registrarPago;
begin
  // GUARDO UN PAGO EN LA TABLA PAGOS
  DataModule1.pago.Append;
  if ComboBox1.ItemIndex = 1 then
  begin
    DataModule1.pago.FieldByName('forma_de_pago').Asstring := 'efectivo';
  end
  else
  begin
    DataModule1.pago.FieldByName('forma_de_pago').Asstring := 'cheque';
    DataModule1.pago.FieldByName('banco').Asstring := LabeledEdit1.Text;
    DataModule1.pago.FieldByName('fecha_cheque').AsDateTime :=
      DateTimePicker1.Date;
    DataModule1.pago.FieldByName('dias').AsInteger := strtoint
      (LabeledEdit4.Text);
  end;
  DataModule1.pago.FieldByName('monto').AsFloat := strtofloat
    (LabeledEdit3.Text);
  DataModule1.pago.FieldByName('fecha').AsDateTime := Date;
  DataModule1.pago.Post;

  // ACTUALIZO EL SALDO DEL CLIENTE
  DataModule1.cliente.Edit;
  DataModule1.cliente.FieldByName('saldo').AsFloat :=
    DataModule1.cliente.FieldByName('saldo').AsFloat - strtofloat
    (LabeledEdit3.Text);
  DataModule1.cliente.Post;
  fcuentaCorriente.cargarCliente;
  fcuentacorriente.cargarPagos;
  self.Close;

end;

end.
